/*
 * @Author: 抱着羊啃 1437670973@qq.com
 * @Date: 2025-10-27 20:02:19
 * @LastEditors: 抱着羊啃 1437670973@qq.com
 * @LastEditTime: 2025-10-28 14:21:16
 * @FilePath: \fall-guard-band\src\main\java\com\example\fall_guard_band\controller\ManagementController.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.example.fall_guard_band.controller;


import com.example.fall_guard_band.pojo.dto.CreateUserRequest;
import com.example.fall_guard_band.pojo.dto.UpdateUserRequest;
import com.example.fall_guard_band.pojo.dto.RoleDTO;
import com.example.fall_guard_band.pojo.dto.CreateRoleRequest;
import com.example.fall_guard_band.pojo.dto.UpdateRoleRequest;
import com.example.fall_guard_band.pojo.dto.UserManagementDTO;
import com.example.fall_guard_band.pojo.result.Result;
import com.example.fall_guard_band.service.UserService;
import com.example.fall_guard_band.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/management")
@Slf4j
@RequiredArgsConstructor
public class ManagementController {
    private final UserService userService;
    private final RoleService roleService;

    // 列表：可按养老院筛选
    @GetMapping("/users")
    public Result<java.util.List<UserManagementDTO>> listUsers(
            @RequestParam(value = "homeId", required = false) Long homeId,
            @RequestParam(value = "isDeleted", required = false) Integer isDeleted
    ) {
        return Result.success(userService.listUsers(homeId, isDeleted));
    }

    // 新增用户
    @PostMapping("/users")
    public Result<Void> createUser(@RequestBody CreateUserRequest req) {
        log.info("Create user: {}", req);
        if (req == null || req.getNursingHomeId() == null) {
            return Result.error("参数不完整：需提供养老院ID");
        }
        boolean ok = userService.createUser(req);
        return ok ? Result.success() : Result.error("创建用户失败：参数或唯一性校验不通过");
    }

    // 编辑用户
    @PutMapping("/users/{id}")
    public Result<Void> updateUser(@PathVariable("id") Long id, @RequestBody UpdateUserRequest req) {
        log.info("Update user id={}, payload={}", id, req);
        if (id == null || req == null) {
            return Result.error("参数不完整：需提供用户ID与更新内容");
        }
        boolean ok = userService.updateUser(id, req);
        return ok ? Result.success() : Result.error("编辑用户失败：校验不通过或用户不存在");
    }

    // 逻辑删除用户
    @DeleteMapping("/users/{id}")
    public Result<Void> deleteUser(@PathVariable("id") Long id) {
        log.info("Delete user id={}", id);
        if (id == null) {
            return Result.error("参数不完整：需提供用户ID");
        }
        boolean ok = userService.deleteUser(id);
        return ok ? Result.success() : Result.error("删除失败：用户不存在");
    }

    // 角色列表查询（可按状态过滤）
    @GetMapping("/roles")
    public Result<java.util.List<RoleDTO>> listRoles(@RequestParam(value = "status", required = false) Integer status) {
        return Result.success(roleService.listRoles(status));
    }

    // 查询指定用户的角色列表
    @GetMapping("/users/{id}/roles")
    public Result<java.util.List<RoleDTO>> listUserRoles(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.error("参数不完整：需提供用户ID");
        }
        return Result.success(roleService.listUserRoles(id));
    }

    // 管理端重置用户密码为 123456
    @PutMapping("/users/{id}/reset-password")
    public Result<Void> resetPassword(@PathVariable("id") Long id) {
        log.info("Admin reset password for user id={}", id);
        if (id == null) {
            return Result.error("参数不完整：需提供用户ID");
        }
        boolean ok = userService.resetUserPassword(id, "123456");
        return ok ? Result.success() : Result.error("重置失败：用户不存在或已删除");
    }

    // 创建角色
    @PostMapping("/roles")
    public Result<Void> createRole(@RequestBody CreateRoleRequest req) {
        log.info("Create role payload={}", req);
        if (req == null || req.getRoleName() == null || req.getRoleName().trim().isEmpty()) {
            return Result.error("参数不完整：需提供角色名称");
        }
        boolean ok = roleService.createRole(req);
        return ok ? Result.success() : Result.error("创建失败：角色名重复或参数非法");
    }

    // 更新角色
    @PutMapping("/roles/{id}")
    public Result<Void> updateRole(@PathVariable("id") Long id, @RequestBody UpdateRoleRequest req) {
        log.info("Update role id={}, payload={}", id, req);
        if (id == null || req == null) {
            return Result.error("参数不完整：需提供角色ID与更新内容");
        }
        boolean ok = roleService.updateRole(id, req);
        return ok ? Result.success() : Result.error("更新失败：角色不存在、名称冲突或参数非法");
    }

    // 删除角色（物理删除）
    @DeleteMapping("/roles/{id}")
    public Result<Void> deleteRole(@PathVariable("id") Long id) {
        log.info("Delete role id={}", id);
        if (id == null) {
            return Result.error("参数不完整：需提供角色ID");
        }
        boolean ok = roleService.deleteRole(id);
        return ok ? Result.success() : Result.error("删除失败：角色不存在");
    }




    







    


}
